home *** CD-ROM | disk | FTP | other *** search
/ PC World 2008 February (DVD) / PCWorld_2008-02_DVD.iso / v cisle / PHP / PHP.exe / xampp-win32-1.6.5-installer.exe / php / PEAR / Log / mail.php < prev    next >
Encoding:
PHP Script  |  2007-12-20  |  7.4 KB  |  258 lines

  1. <?php
  2. /**
  3.  * $Header: /repository/pear/Log/Log/mail.php,v 1.26 2006/12/18 00:53:02 jon Exp $
  4.  *
  5.  * @version $Revision: 1.26 $
  6.  * @package Log
  7.  */
  8.  
  9. /**
  10.  * The Log_mail class is a concrete implementation of the Log:: abstract class
  11.  * which sends log messages to a mailbox.
  12.  * The mail is actually sent when you close() the logger, or when the destructor
  13.  * is called (when the script is terminated).
  14.  *
  15.  * PLEASE NOTE that you must create a Log_mail object using =&, like this :
  16.  *  $logger =& Log::factory("mail", "recipient@example.com", ...)
  17.  *
  18.  * This is a PEAR requirement for destructors to work properly.
  19.  * See http://pear.php.net/manual/en/class.pear.php
  20.  *
  21.  * @author  Ronnie Garcia <ronnie@mk2.net>
  22.  * @author  Jon Parise <jon@php.net>
  23.  * @since   Log 1.3
  24.  * @package Log
  25.  *
  26.  * @example mail.php    Using the mail handler.
  27.  */
  28. class Log_mail extends Log
  29. {
  30.     /**
  31.      * String holding the recipients' email addresses.  Multiple addresses
  32.      * should be separated with commas.
  33.      * @var string
  34.      * @access private
  35.      */
  36.     var $_recipients = '';
  37.  
  38.     /**
  39.      * String holding the sender's email address.
  40.      * @var string
  41.      * @access private
  42.      */
  43.     var $_from = '';
  44.  
  45.     /**
  46.      * String holding the email's subject.
  47.      * @var string
  48.      * @access private
  49.      */
  50.     var $_subject = '[Log_mail] Log message';
  51.  
  52.     /**
  53.      * String holding an optional preamble for the log messages.
  54.      * @var string
  55.      * @access private
  56.      */
  57.     var $_preamble = '';
  58.  
  59.     /**
  60.      * String containing the format of a log line.
  61.      * @var string
  62.      * @access private
  63.      */
  64.     var $_lineFormat = '%1$s %2$s [%3$s] %4$s';
  65.  
  66.     /**
  67.      * String containing the timestamp format.  It will be passed directly to
  68.      * strftime().  Note that the timestamp string will generated using the
  69.      * current locale.
  70.      * @var string
  71.      * @access private
  72.      */
  73.     var $_timeFormat = '%b %d %H:%M:%S';
  74.  
  75.     /**
  76.      * String holding the mail message body.
  77.      * @var string
  78.      * @access private
  79.      */
  80.     var $_message = '';
  81.  
  82.     /**
  83.      * Flag used to indicated that log lines have been written to the message
  84.      * body and the message should be sent on close().
  85.      * @var boolean
  86.      * @access private
  87.      */
  88.     var $_shouldSend = false;
  89.  
  90.     /**
  91.      * Constructs a new Log_mail object.
  92.      *
  93.      * Here is how you can customize the mail driver with the conf[] hash :
  94.      *   $conf['from']    : the mail's "From" header line,
  95.      *   $conf['subject'] : the mail's "Subject" line.
  96.      *
  97.      * @param string $name      The message's recipients.
  98.      * @param string $ident     The identity string.
  99.      * @param array  $conf      The configuration array.
  100.      * @param int    $level     Log messages up to and including this level.
  101.      * @access public
  102.      */
  103.     function Log_mail($name, $ident = '', $conf = array(),
  104.                       $level = PEAR_LOG_DEBUG)
  105.     {
  106.         $this->_id = md5(microtime());
  107.         $this->_recipients = $name;
  108.         $this->_ident = $ident;
  109.         $this->_mask = Log::UPTO($level);
  110.  
  111.         if (!empty($conf['from'])) {
  112.             $this->_from = $conf['from'];
  113.         } else {
  114.             $this->_from = ini_get('sendmail_from');
  115.         }
  116.  
  117.         if (!empty($conf['subject'])) {
  118.             $this->_subject = $conf['subject'];
  119.         }
  120.  
  121.         if (!empty($conf['preamble'])) {
  122.             $this->_preamble = $conf['preamble'];
  123.         }
  124.  
  125.         if (!empty($conf['lineFormat'])) {
  126.             $this->_lineFormat = str_replace(array_keys($this->_formatMap),
  127.                                              array_values($this->_formatMap),
  128.                                              $conf['lineFormat']);
  129.         }
  130.  
  131.         if (!empty($conf['timeFormat'])) {
  132.             $this->_timeFormat = $conf['timeFormat'];
  133.         }
  134.  
  135.         /* register the destructor */
  136.         register_shutdown_function(array(&$this, '_Log_mail'));
  137.     }
  138.  
  139.     /**
  140.      * Destructor. Calls close().
  141.      *
  142.      * @access private
  143.      */
  144.     function _Log_mail()
  145.     {
  146.         $this->close();
  147.     }
  148.  
  149.     /**
  150.      * Starts a new mail message.
  151.      * This is implicitly called by log(), if necessary.
  152.      *
  153.      * @access public
  154.      */
  155.     function open()
  156.     {
  157.         if (!$this->_opened) {
  158.             if (!empty($this->_preamble)) {
  159.                 $this->_message = $this->_preamble . "\r\n\r\n";
  160.             }
  161.             $this->_opened = true;
  162.             $_shouldSend = false;
  163.         }
  164.  
  165.         return $this->_opened;
  166.     }
  167.  
  168.     /**
  169.      * Closes the message, if it is open, and sends the mail.
  170.      * This is implicitly called by the destructor, if necessary.
  171.      *
  172.      * @access public
  173.      */
  174.     function close()
  175.     {
  176.         if ($this->_opened) {
  177.             if ($this->_shouldSend && !empty($this->_message)) {
  178.                 $headers = "From: $this->_from\r\n";
  179.                 $headers .= "User-Agent: Log_mail";
  180.  
  181.                 if (mail($this->_recipients, $this->_subject, $this->_message,
  182.                          $headers) == false) {
  183.                     error_log("Log_mail: Failure executing mail()", 0);
  184.                     return false;
  185.                 }
  186.  
  187.                 /* Clear the message string now that the email has been sent. */
  188.                 $this->_message = '';
  189.                 $this->_shouldSend = false;
  190.             }
  191.             $this->_opened = false;
  192.         }
  193.  
  194.         return ($this->_opened === false);
  195.     }
  196.  
  197.     /**
  198.      * Flushes the log output by forcing the email message to be sent now.
  199.      * Events that are logged after flush() is called will be appended to a
  200.      * new email message.
  201.      *
  202.      * @access public
  203.      * @since Log 1.8.2
  204.      */
  205.     function flush()
  206.     {
  207.         /*
  208.          * It's sufficient to simply call close() to flush the output.
  209.          * The next call to log() will cause the handler to be reopened.
  210.          */
  211.         return $this->close();
  212.     }
  213.  
  214.     /**
  215.      * Writes $message to the currently open mail message.
  216.      * Calls open(), if necessary.
  217.      *
  218.      * @param mixed  $message  String or object containing the message to log.
  219.      * @param string $priority The priority of the message.  Valid
  220.      *                  values are: PEAR_LOG_EMERG, PEAR_LOG_ALERT,
  221.      *                  PEAR_LOG_CRIT, PEAR_LOG_ERR, PEAR_LOG_WARNING,
  222.      *                  PEAR_LOG_NOTICE, PEAR_LOG_INFO, and PEAR_LOG_DEBUG.
  223.      * @return boolean  True on success or false on failure.
  224.      * @access public
  225.      */
  226.     function log($message, $priority = null)
  227.     {
  228.         /* If a priority hasn't been specified, use the default value. */
  229.         if ($priority === null) {
  230.             $priority = $this->_priority;
  231.         }
  232.  
  233.         /* Abort early if the priority is above the maximum logging level. */
  234.         if (!$this->_isMasked($priority)) {
  235.             return false;
  236.         }
  237.  
  238.         /* If the message isn't open and can't be opened, return failure. */
  239.         if (!$this->_opened && !$this->open()) {
  240.             return false;
  241.         }
  242.  
  243.         /* Extract the string representation of the message. */
  244.         $message = $this->_extractMessage($message);
  245.  
  246.         /* Append the string containing the complete log line. */
  247.         $this->_message .= $this->_format($this->_lineFormat,
  248.                                           strftime($this->_timeFormat),
  249.                                           $priority, $message) . "\r\n";
  250.         $this->_shouldSend = true;
  251.  
  252.         /* Notify observers about this log message. */
  253.         $this->_announce(array('priority' => $priority, 'message' => $message));
  254.  
  255.         return true;
  256.     }
  257. }
  258.